package com.yi.student.util;

import java.text.DateFormat;
import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.Date;
import java.util.Iterator;
import java.util.List;

import org.konghao.poi.util.ExcelUtil;

import com.yi.student.dto.ExcelDto;
import com.yi.student.model.Student;

public class WebUtil {
	private static int count = 1000;
	/**
	 * 获取上传文件的绝对路径
	 * @return
	 */
	public static String getUploadPath(){
		String path = "" ;
		path = Thread.currentThread().getContextClassLoader().getResource("").toString();

		int index = path.indexOf("WEB-INF") ;
		path = path.substring(0 , index) + "resource/upload";
		path = path.replace("file:/", "");
		return path;
	}
	
	public static List<Student> transExcel2Student(String path){
		List<Student> stus = new ArrayList<Student>();
		List<Object> objs = ExcelUtil.getInstance().readExcel2ObjsByPath(path, ExcelDto.class);
		Iterator<Object> iter = objs.iterator() ;
		while(iter.hasNext()){
			ExcelDto ed = (ExcelDto)iter.next() ;
			stus.add(ed.transStudentExcel());
		}
		
		return stus;
	}
	
	
	public static String fileName(){
		String filename = formatNowDate()+ count;
		if(count ++ == 9999){
			count = 0 ;
		}
		return filename ;
	}
	
	public static String formatNowDate(){
		String now = "" ;
		DateFormat df = new SimpleDateFormat("yyyyMMddHHmmssS");
		now = df.format(new Date());
		return now ;
	}
	
	public static String formatDate2String(Date date){
		String timeString = "";
		DateFormat sf = new SimpleDateFormat("yyyyMMddHHmmss");
		timeString = sf.format(date) ;
		return timeString;
	} 
	  
	
	public static void main(String[] args){
		System.out.println(getUploadPath());
	}

}
